<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:p="http://primefaces.org/ui"
      xmlns:f="http://java.sun.com/jsf/core">

    <ui:composition template="layout.xhtml">
        <ui:define name="body">


            <h:form id="incomeCreate" enctype="multipart/form-data">
                <h1 class="title">
                    <a href="#" class="back" onclick="history.go(-1)"/>
                    Laudo
                </h1>
                <p:messages showDetail="true" autoUpdate="true"  /> 
                <div class="row">
                    <div class="twelve columns">
                        <fieldset>
                            <legend> <h4 style="margin:0 0 10px 0">Laudo</h4></legend>
                            <p:panel id="findPanel" style="background: none; border: none;" 
                                     styleClass="container_24 clearfix " >

                                <p:outputLabel for="ficha" value="Ficha: " styleClass="grid_3"/> 
                                <p:inputText id="ficha" styleClass="grid_5" disabled="#{managerAppraisal.entregar}" readonly="#{managerAppraisal.edit ne null}"
                                             value="#{managerAppraisal.idDiary}">
                                    <p:ajax event="blur" disabled="#{managerAppraisgeralal.edit ne null}" 
                                            update="@form" listener="#{managerAppraisal.loadDiary()}"/>
                                </p:inputText>

                                <p:commandButton value="Entregar Laudo" icon="ui-icon-alert"
                                                 process="@this" update="@this"
                                                 rendered="#{managerAppraisal.entregar and managerAppraisal.appraisal.systemUserAppraisal eq null ? true:false}"
                                                 onclick="dlgDeliveredAppraisal.show();"
                                                 styleClass="ui-priority-primary right" />
                                <p:button value="Editar" icon="ui-icon-pencil"
                                          outcome="createAppraisal.xhtml"
                                          rendered="#{managerAppraisal.entregar}"
                                          styleClass="right" >
                                    <f:param name="backedit" value="true" />
                                    <f:param name="appraisalEdit" value="#{managerAppraisal.appraisal.id}" />
                                </p:button>

                                <p:outputLabel rendered="false" value="Situação: " styleClass="grid_3"/> 
                                <div class="grid_5">
                                    <p:selectOneMenu  rendered="false" style="width: 108%;" panelStyle="width:150px;" 
                                                      value="#{managerAppraisal.appraisal.situationAppraisal}" >   
                                        <f:selectItems value="#{managerAppraisal.selectSituationAppraisal()}" />
                                    </p:selectOneMenu>
                                </div>
                            </p:panel>
                            <p:panel id="panelEntregue" rendered="#{managerAppraisal.appraisal.situationAppraisal eq 'DELIVERED'}" style="background: none; border: none;" 
                                     styleClass="container_24 clearfix " >

                                <div class="clear"/>
                                <p:outputLabel  value="Recebido por: " styleClass="grid_3"/> 
                                <p:inputText disabled="true"  styleClass="grid_10" 
                                             value="#{managerAppraisal.appraisal.recipientName}"/>

                                <p:outputLabel  value="Documento: " styleClass="grid_3"/> 
                                <p:inputText disabled="true" styleClass="grid_6" style="width: 22%;" 
                                             value="#{managerAppraisal.appraisal.recipientDoc}"/>
                                <div class="clear"/>
                                <p:outputLabel  value="Entregue por: " styleClass="grid_3"/> 
                                <p:inputText disabled="true"  styleClass="grid_19" 
                                             value="#{managerAppraisal.appraisal.systemUserAppraisal.name}"/>
                                <div class="clear"/>
                                <p:outputLabel  value="em: " styleClass="grid_3"/> 
                                <p:inputText disabled="true" styleClass="grid_5" 
                                             value="#{managerAppraisal.appraisal.dateDelivery}">
                                    <f:convertDateTime locale="pt,BR" pattern="dd/MM/yyyy HH:mm:ss"/>
                                </p:inputText>




                            </p:panel>

                        </fieldset>
                        <fieldset>
                            <legend> <h4 style="margin:0 0 10px 0">Exame</h4></legend>
                            <p:panel id="diaryPanel" style="background: none; border: none;" 
                                     styleClass="container_24 clearfix " >

                                <p:outputLabel for="exame" value="Exame: " styleClass="grid_4"/> 
                                <p:inputText id="exame" disabled="#{managerAppraisal.entregar}"  
                                             styleClass="grid_18" 
                                             value="#{managerAppraisal.appraisal.title}"/>
                                <p:outputLabel for="medicorequisitante" value="Médico Requisitante: " styleClass="grid_4"/> 
                                <p:inputText id="medicorequisitante" disabled="#{managerAppraisal.entregar}"
                                             styleClass="grid_18" readonly="true"
                                             value="#{managerAppraisal.diary.doctorRequest.name}"/>

                                <p:outputLabel for="doctor" value="Médico Responsável: " styleClass="grid_4"/>  
                                <p:inputText value="#{managerAppraisal.doctor.name}" disabled="#{managerAppraisal.entregar}"
                                             id="doctor" readonly="true"  styleClass="grid_17"
                                             style="margin-right:0px;"/>

                                <p:commandButton  icon="ui-icon-plus" type="button" disabled="#{managerAppraisal.entregar}"
                                                  styleClass="ui-button-icon-only grid_2" rendered="#{!managerFinancial.isDoctor}"
                                                  style="width: 25px; height: 28px; "   
                                                  onclick="dlgDoctorDialog.show()"/>

                                <div class="clear"/>

                                <p:outputLabel for="customer" value="Paciente: " styleClass="grid_4"/> 
                                <p:inputText id="customer" disabled="#{managerAppraisal.entregar}"  styleClass="grid_18" readonly="true"
                                             value="#{managerAppraisal.diary.customer.name}"/>
                                <p:outputLabel for="operator" value="Convênio: " styleClass="grid_4"/> 
                                <p:inputText id="operator" disabled="#{managerAppraisal.entregar}"   styleClass="grid_18" readonly="true"
                                             value="#{managerAppraisal.diary.operator.name}"/>



                            </p:panel>
                        </fieldset>
                        <fieldset>
                            <legend> <h4 style="margin:0 0 10px 0">Laudo</h4></legend>
                            <p:panel id="appraisalPanel" style="background: none; border: none;" 
                                     styleClass="container_24 clearfix " >

                                <h:outputText value="Selecione o Modelo de Laudo" 
                                              styleClass="grid_8" style="color:red;"
                                              rendered="#{managerAppraisal.templateAppraisal eq null}"/>
                                <div class="clear"/>
                                <div class="grid_7 template_appraisal_pick" style="padding: 5px;">
                                    <div class="clear"/>
                                    <div style="text-align: center; font-weight: bold; font-size: 1.3em;" class="grid_23">
                                        #{managerAppraisal.entregar ? '':'Modelo'}
                                    </div>

                                    <div class="clear"/>
                                    <p:inputText id="templateAppraisal"
                                                 rendered="#{!managerAppraisal.entregar}"  styleClass="grid_20" readonly="true"
                                                 value="#{managerAppraisal.templateAppraisal.name}"/>

                                    <p:commandButton  rendered="#{!managerAppraisal.entregar}"  icon="ui-icon-plus" styleClass="grid_3" 
                                                      style="height: 28px; margin-left: -3px;"
                                                      onclick="dlgModeloDialog.show()"/>


                                    <div class="clear"/>


                                    <p:treeTable 
                                        rendered="#{!managerAppraisal.entregar}" 
                                        value="#{managerAppraisal.root}" var="document" id="multiSelect"

                                        selectionMode="checkbox" selection="#{managerAppraisal.selectedNodes}">

                                        <p:ajax event="select" listener="#{managerAppraisal.onNodeSelect}"
                                                update=":incomeCreate:editorAppraisal"/>
                                        <p:ajax event="unselect" listener="#{managerAppraisal.onNodeUnselect}"
                                                update=":incomeCreate:editorAppraisal"/>

                                        <f:facet name="header" >
                                            <h:outputText value="#{managerAppraisal.templateAppraisal.name}" 
                                                          rendered="#{managerAppraisal.templateAppraisal ne null}"/>
                                            <h:outputText value="-" 
                                                          rendered="#{managerAppraisal.templateAppraisal eq null}"/>
                                        </f:facet>
                                        <p:column style="width:150px; white-space: pre-wrap;" >
                                            <h:outputText value="#{document.titulo}" />
                                        </p:column>

                                    </p:treeTable>

                                </div>

                                <p:commandButton value="Buscar Frases" icon="ui-icon-search"
                                                 process="@this" 
                                                 update="@this"
                                                 onclick="dlgFraseDialog.show();"
                                                 style="margin-bottom: 10px;" />

                                <p:commandButton id="backTextBtn" 
                                                 rendered="false" 
                                                 value="Versão Original" icon="ui-icon-arrowreturnthick-1-w"
                                                 process="@this, :incomeCreate:appraisalPanel" 
                                                 update="@this, :incomeCreate:appraisalPanel"
                                                 actionListener="#{managerAppraisal.backTextOrigin()}"
                                                 style="margin-bottom: 10px;"
                                                 styleClass="right" />

                                <p:commandButton id="salvarmodelo" 
                                                 rendered="#{!managerAppraisal.entregar}" 
                                                 value="Salvar como Modelo" icon="ui-icon-disk"
                                                 process="@this, :incomeCreate:appraisalPanel" 
                                                 update="@this, :incomeCreate:appraisalPanel"
                                                 onclick="dlgsalvarmodelo.show();"
                                                 style="margin-bottom: 10px;"
                                                 styleClass="right" />


                                <p:commandButton 
                                    value="Imprimir" icon="ui-icon-print" 
                                    ajax="false" process="@form"  onclick="this.form.target='_blank'"
                                    styleClass="ui-priority-primary right" style="margin-bottom: 10px;" action="#{managerAppraisal.print()}"/>

                                <p:blockUI block="appraisalPanel" trigger="backTextBtn">
                                    CARREGANDO<br />
                                    <p:graphicImage value="/assets/images/ajax-loader.gif"/><br/>
                                    Por favor aguarde alguns instantes !
                                </p:blockUI>
                                <div class="#{managerAppraisal.entregar ? 'grid_22':'grid_16'}">
                                    <p:editor id="editorAppraisal" 
                                              disabled="#{managerAppraisal.entregar}"
                                              style="width: 106%" height="450" 
                                              value="#{managerAppraisal.text}"/>
                                </div>
                            </p:panel>


                        </fieldset>

                        <fieldset>
                            <legend> <h4 style="margin:0 0 10px 0">Observações</h4></legend>
                            <p:panel id="appraisalObsPanel" style="background: none; border: none;" 
                                     styleClass="container_24 clearfix " >
                                <h:outputText value="Observações: " styleClass="grid_4"/> 
                                <p:inputTextarea disabled="#{managerAppraisal.entregar}" styleClass="grid_23"  value="#{managerAppraisal.appraisal.obs}"/>
                            </p:panel>

                        </fieldset>
                        <br/>
                        <p:selectBooleanCheckbox disabled="#{managerAppraisal.entregar}"  styleClass="grid_2" value="#{managerAppraisal.finalizar}" />
                        <h:outputText styleClass="grid_16" value="Desejo finalizar a edição do Laudo e disponibilizar para entrega." />

                        <div class="clear"/>
                        <div class="buttonAction">
                            <p:commandButton value="Salvar" icon="ui-icon-disk"
                                             process="@form" update="@form" rendered="#{!managerAppraisal.entregar}"
                                             actionListener="#{managerAppraisal.save()}"
                                             styleClass="ui-priority-primary" />

                            <p:button value="Voltar" icon="ui-icon-arrowreturn-1-w"
                                      outcome="indexAppraisal.xhtml"/>

                        </div>

                    </div>
                </div>

            </h:form>
            <h:form id="formdlg">
                <p:dialog id="dlgsalvarmodelo"  widgetVar="dlgsalvarmodelo"
                          header="Novo Modelo" modal="true"
                          width="700" height="200"  dynamic="true" > 
                    <p:messages autoUpdate="true"/>
                    <p:panel id="panelModelo" style="background: none; border: none;" 
                             styleClass="container_24 clearfix " >
                        <p:outputLabel  value="Nome: " styleClass="grid_3"/> 
                        <p:inputText  styleClass="grid_19" 
                                      value="#{managerAppraisal.novoTemplateAppraisal.name}"/>
                        <div class="clear"/>
                        <div class="buttonAction">
                            <p:commandButton value="Salvar" icon="ui-icon-search"
                                             actionListener="#{managerAppraisal.salvarModelo()}"
                                             process="@this, :formdlg:panelModelo" update="@this, :formdlg:panelModelo"
                                             oncomplete="dlgsalvarmodelo.hide();"
                                             styleClass="ui-priority-primary" />
                        </div>
                    </p:panel>
                </p:dialog>
                <p:dialog id="dlgEntregaLaudo"  widgetVar="dlgDeliveredAppraisal"
                          header="Controle de Entrega" modal="true"
                          width="700" height="250"  dynamic="true" > 

                    <p:panel id="panelEntrega" style="background: none; border: none;" 
                             styleClass="container_24 clearfix " >

                        <h:outputText  value="Entrego o Laudo do exame #{managerAppraisal.appraisal.diary.procedure.name} realizado pelo paciente 
                                       #{managerAppraisal.appraisal.customer.name} à" 
                                       styleClass="grid_23"/> 
                        <div class="clear"/>
                        <br/>
                        <div class="clear"/>
                        <p:outputLabel  value="Nome: " styleClass="grid_3"/> 
                        <p:inputText  styleClass="grid_19" 
                                      value="#{managerAppraisal.appraisal.recipientName}"/>
                        <p:outputLabel  value="Documento: " styleClass="grid_3"/> 
                        <p:inputText  styleClass="grid_19" 
                                      value="#{managerAppraisal.appraisal.recipientDoc}"/>


                        <br/>
                        <br/>
                        <div class="clear"/>
                        <div class="buttonAction">
                            <p:commandButton value="Salvar" icon="ui-icon-search"
                                             actionListener="#{managerAppraisal.entregarLaudo()}"
                                             process="@this, :formdlg:panelEntrega" update="@this, :formdlg:panelEntrega"
                                             styleClass="ui-priority-primary" />
                        </div>

                    </p:panel>
                </p:dialog>

                <p:dialog id="dlgModelo"  widgetVar="dlgModeloDialog"
                          header="Modelo Laudo" modal="true"
                          width="800" height="400"  dynamic="true" > 

                    <p:panel id="panelModel" style="background: none; border: none;" 
                             styleClass="container_24 clearfix " >

                        <p:outputLabel  value="Nome: " styleClass="grid_3"/> 
                        <p:inputText  styleClass="grid_19" 
                                      value="#{managerAppraisal.searchName}"/>


                        <div class="buttonAction">
                            <p:commandButton value="Pesquisar" icon="ui-icon-search"
                                             actionListener="#{managerAppraisal.searchTemplate()}"
                                             process="@this, :formdlg:panelModel" update="@this, :formdlg:panelModel"
                                             styleClass="ui-priority-primary" />
                        </div>
                        <p:dataTable id="tableItem" var="ta" value="#{managerAppraisal.templateAppraisals}" 
                                     styleClass="grid_24"
                                     rowIndexVar="rowid"
                                     rowKey="#{ta.id}"
                                     rows="5" paginator="true"  
                                     paginatorPosition="bottom"
                                     emptyMessage="Nenhum registro">  

                            <f:facet name="header">
                                Modelos
                            </f:facet>

                            <p:column headerText="Selecionar" width="5" style="text-align: center;">

                                <p:commandButton icon="ui-icon-search" alt="Visualizar Modelo" title="Visualizar Modelo"
                                                 process="@this, :incomeCreate:appraisalPanel" update="@this, :incomeCreate:appraisalPanel"
                                                 actionListener="#{managerAppraisal.addTemplate(rowid)}"
                                                 oncomplete="dlgModeloDialog.hide();"
                                                 style="width: 25px; position: relative;"/>

                            </p:column> 
                            <p:column headerText="Nome" style="white-space: pre-wrap;">  
                                <h:outputText value="#{ta.name}"/>
                            </p:column>  

                        </p:dataTable>

                    </p:panel>
                </p:dialog>

                <p:dialog id="dlgFrase"  widgetVar="dlgFraseDialog"
                          header="Buscar Frase" modal="true" closeOnEscape="true"
                          width="800" height="400"  dynamic="false" > 

                    <p:panel id="panelFrase" style="background: none; border: none;" 
                             styleClass="container_24 clearfix " >

                        <p:outputLabel value="Trecho Frase: " styleClass="grid_4"/> 
                        <p:inputText styleClass="grid_18" 
                                     value="#{managerAppraisal.frase}"/>


                        <div class="buttonAction">
                            <p:commandButton value="Pesquisar" icon="ui-icon-search"
                                             actionListener="#{managerAppraisal.pesquisarFrase()}"
                                             process="@this, :formdlg:panelFrase" update="@this, :formdlg:panelFrase"
                                             styleClass="ui-priority-primary" />


                        </div>
                        <p:dataTable id="tablefrases" var="fr" value="#{managerAppraisal.frases}" 
                                     styleClass="grid_24"
                                     rowIndexVar="rowid"
                                     rowKey="#{fr.id}"
                                     rows="5" paginator="true"  
                                     paginatorPosition="bottom"
                                     emptyMessage="Nenhum registro">  

                            <f:facet name="header">
                                Frases
                            </f:facet>

                            <p:column headerText="Descrição" style="white-space: pre-wrap;">
                                <p:inplace effectSpeed="fast">
                                    <p:inputText value="#{fr.descricao}"/>
                                </p:inplace>
                            </p:column>  

                        </p:dataTable>

                    </p:panel>
                </p:dialog>

                <p:confirmDialog id="dlgPrint"  widgetVar="dlgPrint"
                                 header="Salva com sucesso" message="Deseja Imprimir o laudo ??" 
                                 > 
                    <div class="buttonAction">
                        <p:commandButton 
                            value="Sim" icon="ui-icon-print" 
                            ajax="false" process="@form"  onclick="this.form.target='_blank'"
                            oncomplete="dlgPrint.hide();"
                            styleClass="ui-priority-primary" action="#{managerAppraisal.print()}"/>

                        <p:commandButton value="Não" onclick="dlgPrint.hide();" icon="ui-icon-close"/>    
                    </div>
                </p:confirmDialog>

                <p:dialog id="dlgDoctor"  widgetVar="dlgDoctorDialog"
                          header="Pesquisar Médico" modal="true"
                          width="800"  dynamic="true" > 

                    <p:ajax event="close" update="@this, :incomeCreate:diaryPanel" />

                    <p:messages autoUpdate="true" closable="true"/>

                    <p:panel id="medicoPanelSearch" style="background: none; border: none;" 
                             styleClass="container_24 clearfix ">

                        <p:outputLabel value="Nome:" styleClass="grid_3"/>
                        <p:inputText styleClass="grid_20" value="#{managerAppraisal.nameDoctor}"/>

                        <div class="buttonAction">
                            <p:commandButton value="Pesquisar" icon="ui-icon-search"
                                             update="medicoPanelSearch" process="medicoPanelSearch"
                                             actionListener="#{managerAppraisal.listDoctor()}"
                                             styleClass="ui-priority-primary" />
                        </div >

                        <p:dataTable var="dct" id="doctorDataTable" value="#{managerAppraisal.doctors}"  
                                     style="white-space: pre-wrap;" selectionMode="single" rowKey="#{dct.id}"
                                     selection="#{managerAppraisal.doctorSelected}"
                                     emptyMessage="Nenhum registro encontrado" sortMode="multiple"
                                     paginatorPosition="bottom" paginator="true" rows="5"
                                     >

                            <p:ajax event="rowSelect"  process="@this" update="@this"/>
                            <p:ajax event="rowUnselect"  process="@this" update="@this"/>

                            <f:facet name="header">
                                Médicos
                            </f:facet> 
                            <p:ajax event="rowSelect"  process="@this" update="@this"/>
                            <p:ajax event="rowUnselect"  process="@this" update="@this"/>
                            <p:column  headerText="CBO" >  
                                <h:outputText value="#{dct.cbo.descr}" />  
                            </p:column>
                            <p:column  headerText="Nome" >  
                                <h:outputText value="#{dct.name}" />  
                            </p:column>

                        </p:dataTable>

                        <div class="buttonAction">

                            <p:commandButton value="Selecionar" icon="ui-icon-search"
                                             update="@this, :incomeCreate:diaryPanel" 
                                             process="@this"
                                             actionListener="#{managerAppraisal.selectDoctor()}"
                                             oncomplete="dlgDoctorDialog.hide();" />

                            <p:commandButton value="Remover" icon="ui-icon-trash"
                                             update="@this, :incomeCreate:diaryPanel" 
                                             process="@this" 
                                             oncomplete="dlgDoctorDialog.hide();"
                                             immediate="false" actionListener="#{managerAppraisal.clearDoctor()}" />
                        </div>
                    </p:panel>
                </p:dialog>


            </h:form>
        </ui:define>
    </ui:composition>
</html>
